package com.zero.lite.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zero.lite.dao.entity.GoodsSpecification;
import com.zero.lite.dao.mapper.GoodsSpecificationMapper;
import com.zero.lite.dao.service.IGoodsSpecificationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品对应规格表值表 服务实现类
 * </p>
 *
 * @author zero
 * @since 2018-05-04
 */
@Service
public class GoodsSpecificationServiceImpl extends ServiceImpl<GoodsSpecificationMapper, GoodsSpecification> implements IGoodsSpecificationService {
    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public Object getSpecificationVoList(Integer id) {

        EntityWrapper ew=new EntityWrapper();
        ew.eq("good_id",id);
        List<GoodsSpecification> goodsSpecificationList =goodsSpecificationMapper.selectList(ew);

        Map<String, VO> map = new HashMap<>();
        List<VO> specificationVoList = new ArrayList<>();

        for(GoodsSpecification goodsSpecification : goodsSpecificationList){
            String specification = goodsSpecification.getSpecification();
            VO goodsSpecificationVo = map.get(specification);
            if(goodsSpecificationVo == null){
                goodsSpecificationVo = new VO();
                goodsSpecificationVo.setName(specification);
                List<GoodsSpecification> valueList = new ArrayList<>();
                valueList.add(goodsSpecification);
                goodsSpecificationVo.setValueList(valueList);
                map.put(specification, goodsSpecificationVo);
                specificationVoList.add(goodsSpecificationVo);
            }
            else{
                List<GoodsSpecification> valueList = goodsSpecificationVo.getValueList();
                valueList.add(goodsSpecification);
            }
        }

        return specificationVoList;
    }

    private class VO {
        private String name;
        private List<GoodsSpecification> valueList;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public List<GoodsSpecification> getValueList() {
            return valueList;
        }

        public void setValueList(List<GoodsSpecification> valueList) {
            this.valueList = valueList;
        }
    }
}
